<template>
  <a-layout id="components-layout-demo-custom-trigger" class="main">
    <a-layout-sider v-model:collapsed="collapsed" :trigger="null" collapsible>
      <div class="logo" />
      <a-menu theme="dark" mode="inline" v-model:selectedKeys="selectedKeys">
        <Menu :userRole="userRole" @menuChange="clickMenu"></Menu>
      </a-menu>
    </a-layout-sider>
    <a-layout>
      <a-layout-header style="background: #fff; padding: 0">
        <menu-unfold-outlined v-if="collapsed" class="trigger" @click="() => (collapsed = !collapsed)" />
        <menu-fold-outlined v-else class="trigger" @click="() => (collapsed = !collapsed)" />
        <a-button class="back" type="primary" @click="backToLogin">返回登录页</a-button>
      </a-layout-header>
      <a-breadcrumb style="margin: 2px 0px 0px 16px">
        <a-breadcrumb-item>主页</a-breadcrumb-item>
        <a-breadcrumb-item v-if="userRole == 0">{{ menuItemList1[menuIndex] }}</a-breadcrumb-item>
        <a-breadcrumb-item v-else>{{ menuItemList2[menuIndex] }}</a-breadcrumb-item>
      </a-breadcrumb>
      <a-layout-content :style="{ margin: '5px 16px', padding: '24px', background: '#fff', minHeight: '280px' }">
        <router-view></router-view>
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>
<script>
import { UserOutlined, VideoCameraOutlined, UploadOutlined, MenuUnfoldOutlined, MenuFoldOutlined, FormOutlined, IssuesCloseOutlined, UnorderedListOutlined } from '@ant-design/icons-vue'
import { useStore } from 'vuex'
import { defineComponent, ref } from 'vue'
import { useRouter } from 'vue-router'
import Menu from '@/components/Menu'
import roles from '@/utils/menu'
export default defineComponent({
  components: {
    UserOutlined,
    VideoCameraOutlined,
    UploadOutlined,
    MenuUnfoldOutlined,
    MenuFoldOutlined,
    FormOutlined,
    IssuesCloseOutlined,
    UnorderedListOutlined,
    Menu,
  },
  setup() {
    const store = useStore()
    const router = useRouter()
    const userRole = ref(store.state.role.role - 1)
    const hasPermission = function (permission) {
      const role = this.roles.find((role) => role.name === this.userRole)
      return role.permissions.includes(permission)
    }
    const menuItemList1 = ['请假申请', '请假记录']
    const menuItemList2 = ['请假申请', '请假审批', '请假记录']
    const menuIndex = ref(0)
    const clickMenu = (index) => {
      menuIndex.value = index
    }
    const selectedKeys = ref(['1'])
    const backToLogin = () => {
      router.replace('/')
    }
    return {
      hasPermission,
      selectedKeys,
      collapsed: ref(false),
      userRole,
      roles,
      clickMenu,
      menuItemList1,
      menuItemList2,
      menuIndex,
      backToLogin,
    }
  },
})
</script>
<style>
.main {
  height: 100%;
}
#components-layout-demo-custom-trigger .trigger {
  font-size: 18px;
  line-height: 64px;
  padding: 0 24px;
  cursor: pointer;
  transition: color 0.3s;
}

#components-layout-demo-custom-trigger .trigger:hover {
  color: #1890ff;
}

#components-layout-demo-custom-trigger .logo {
  background-color: rgba(255, 255, 255, 0.3);
  margin: 16px;
}

.site-layout .site-layout-background {
  background: #fff;
}
.menu-item {
  border-radius: 0 !important;
}
.back {
  position: relative;
  left: 85%;
}
</style>
